﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;

namespace PDFDesign.Model
{
    public class NewStyle
    {
        [JsonProperty("pageSetting")]
        public PageSetting PageSetting { get; set; }
        [JsonProperty("controls")]
        public List<Control> Controls { get; set; }
        [JsonProperty("table")]
        public Table Table { get; set; }
    }

    public class PageSetting
    {
        [JsonProperty("size")]
        public double[] Size { get; set; }
        [JsonProperty("layout")]
        [JsonConverter(typeof(StringEnumConverter))]
        public Layout Layout { get; set; } = Layout.portrait;
        [JsonProperty("margins")]
        public Margins Margins { get; set; }
        [JsonProperty("reportCount")]
        public int ReportCount { get; set; }
    }

    public class Margins
    {
        [JsonProperty("top")]
        public double Top { get; set; }
        [JsonProperty("left")]
        public double Left { get; set; }
        [JsonProperty("bottom")]
        public double Bottom { get; set; }
        [JsonProperty("right")]
        public double Right { get; set; }
    }

    public class Table
    {
        [JsonProperty("fixedRowCount")]
        public bool FixedRowCount { get; set; }
        [JsonProperty("rowCount")]
        public int RowCount { get; set; }
        [JsonProperty("renderWhiteRow")]
        public bool RenderWhiteRow { get; set; }
        [JsonProperty("position")]
        public Position Position { get; set; }
        [JsonProperty("size")]
        public Size Size { get; set; }
        [JsonProperty("fixedRows")]
        public List<Row> FixedRows { get; set; }
        [JsonProperty("columns")]
        public List<Column> Columns { get; set; }
        [JsonProperty("rows")]
        public List<Row> Rows { get; set; }
    }

    public class Position
    {
        [JsonProperty("top")]
        public double Top { get; set; }
        [JsonProperty("left")]
        public double Left { get; set; }
    }

    public class Size
    {
        [JsonProperty("width")]
        public double Width { get; set; }
        [JsonProperty("height")]
        public double Height { get; set; }
    }

    public class Cell
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("origin")]
        [JsonConverter(typeof(StringEnumConverter))]
        public DataOrigin Origin { get; set; } = DataOrigin.value;
        [JsonProperty("value")]
        public string Value { get; set; }
        [JsonProperty("columnSpan")]
        public int ColumnSpan { get; set; }
        [JsonProperty("dataField")]
        public string DataField { get; set; }
        [JsonProperty("merger")]
        public bool Merger { get; set; }
        [JsonProperty("groupName")]
        public string GroupName { get; set; }
        [JsonProperty("textStyle")]
        public Textstyle TextStyle { get; set; }

        public static Cell Default(string name) => new Cell
        {
            Name = name,
            Origin = DataOrigin.value,
            Value = "",
            ColumnSpan = 0,
            DataField = "",
            Merger = false,
            GroupName = "",
            TextStyle = Textstyle.Default
        };
    }

    public class Textstyle
    {
        [JsonProperty("font")]
        public Font Font { get; set; }
        [JsonProperty("align")]
        [JsonConverter(typeof(StringEnumConverter))]
        public Align Align { get; set; } = Align.left;
        [JsonProperty("valign")]
        [JsonConverter(typeof(StringEnumConverter))]
        public VAlign VAlign { get; set; } = VAlign.top;
        [JsonProperty("margins")]
        public Margins Margins { get; set; }

        public static Textstyle Default => new Textstyle
        {
            Align = Align.left,
            VAlign = VAlign.top,
            Font = new Font { Size = 12, Name = FontName.normal },
            Margins = new Margins { Top = 0, Bottom = 0, Left = 0, Right = 0 }
        };
    }

    public class Font
    {
        [JsonProperty("name")]
        [JsonConverter(typeof(StringEnumConverter))]
        public FontName Name { get; set; } = FontName.normal;
        [JsonProperty("size")]
        public int Size { get; set; }
    }

    public class Column
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("fixedWidth")]
        public bool FixedWidth { get; set; }
        [JsonProperty("width")]
        public double Width { get; set; }
        [JsonProperty("precent")]
        public double Precent { get; set; }

        public static Column Default(string name) => new Column
        {
            Name = name,
            FixedWidth = false,
            Precent = 10,
            Width = 0
        };
    }

    public class Row
    {
        [JsonProperty("type")]
        [JsonConverter(typeof(StringEnumConverter))]
        public RowType Type { get; set; } = RowType.normal;
        [JsonProperty("multHeader")]
        public bool MultHeader { get; set; }
        [JsonProperty("fixedRowHeight")]
        public bool FixedRowHeight { get; set; }
        [JsonProperty("height")]
        public double Height { get; set; }
        [JsonProperty("cells")]
        public List<Cell> Cells { get; set; }

        public static Row Default => new Row
        {
            Type = RowType.normal,
            MultHeader = false,
            FixedRowHeight = false,
            Height = 0,
            Cells = new List<Cell>()
        };
    }

    public class Control
    {
        [JsonProperty("type")]
        [JsonConverter(typeof(StringEnumConverter))]
        public ControlType Type { get; set; } = ControlType.text;
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("caption")]
        public string Caption { get; set; }
        [JsonProperty("showCaption")]
        public bool ShowCaption { get; set; }
        [JsonProperty("origin")]
        [JsonConverter(typeof(StringEnumConverter))]
        public DataOrigin Origin { get; set; } = DataOrigin.value;
        [JsonProperty("dataField")]
        public string DataField { get; set; }
        [JsonProperty("isPageNumber")]
        public bool IsPageNumber { get; set; }
        [JsonProperty("value")]
        public string Value { get; set; }
        [JsonProperty("relativeTable")]
        public bool RelativeTable { get; set; }
        [JsonProperty("position")]
        public Position Position { get; set; }
        [JsonProperty("size")]
        public Size Size { get; set; }
        [JsonProperty("textStyle")]
        public Textstyle TextStyle { get; set; }

        public static Control Default(string name) => new Control
        {
            Name = name,
            Caption = name,
            Origin = DataOrigin.value,
            Position = new Position { Left = 0, Top = 0 },
            DataField = "",
            ShowCaption = false,
            Size = new Size { Width = 100, Height = 30 },
            Type = ControlType.text,
            Value = "",
            RelativeTable = false,
            IsPageNumber = false,
            TextStyle = Textstyle.Default
        };
    }

    public enum FontName
    {
        normal, blod, thin
    }

    public enum Align
    {
        left, center, right, justify
    }

    public enum VAlign
    {
        top, middle, bottom
    }

    public enum RowType
    {
        header, detail, normal
    }

    public enum DataOrigin
    {
        value, master, detail, sumtext
    }

    public enum ControlType
    {
        text, image, qrcode, barcode
    }

    public enum Layout
    {
        portrait,
        landscape
    }
}
